import json
import cv2
from loguru import logger
from typing import Type
from sche_task.sound import play_dianjian_sound
from cv.dev.dev_window import handle_dev_window
from cv.dev.dev_window_long import handle_dev_window_long_camera
from cv.dev.red_temp_blue_humi import handle_red_temp_blue_humi
from cv.dev.red_temp_blue_humi_2 import handle_red_temp_blue_humi_2
from cv.dev.black_bg_white_temp_humi import handle_black_bg_white_temp_humi
from cv.dev.black_bg_white_temp_humi_two import handle_black_bg_white_temp_humi_two

# from sche_task import schedulerIns
# from sio_agv.agv_socket import AGV
# from app.core.conf import ST
from sio import sio
# from app.api.service import dev_service
from app.api.service import station_service 

# from cv.image.BaseImg import BaseImg
# from cv.filter.filter import Filter
# from cv.classify.classify_state import ClassifyState
# from cv.classify.classify_th import ClassifyTH
# from rasip.camera import get_rasip_photo
# from sio.agv import send_cvinfo
# from app.api.service import img_service
# from app.api.service import qm_service
from app.utils.generate_string import get_cur_timestamp

# 第一次运行的flag
first_run = True
# 当前站点,并不是最近的站点，而是用来CV识别的站点
cur_station=''


async def handle_nav_status_to_photo(all1):
    global cur_station
    global first_run
    task_status = all1.get('task_status')#站点的导航状态 
    target_id = all1.get('target_id') # 站点名称

    # 正在前往站点
    if task_status ==2 :
        logger.debug('正在前往站点'+target_id) 
        # await sio.emit("cv_rm")
        # 正在导航,说明前往新的站点,这个时候需要重置first_run flag
        # first_run = True
    # 导航暂停中
    elif task_status ==3:
        logger.debug('导航暂停中'+target_id)
    # 到达站点
    elif task_status==4: 
        # 如果当前站点不等于存储的站点,说明到了一个新的站点,这个时候需要重置first_run
        if cur_station!=target_id:
            first_run = True
        # 如果不是第一次运行,则返回
        if not first_run:
            return
        logger.debug('到达站点(只运行一次)'+target_id)

        # 创建一个线程用来处理CV识别
        continue_flag = await handle_camera(target_id)
        #  如果返回为True，则还需要继续识别一遍，返回False，则不需要在识别一遍
        first_run = False or continue_flag
        cur_station = target_id 
    # 导航失败
    elif task_status==5:
        logger.debug('前往站点,失败:'+target_id)
    # 取消导航
    elif task_status==6:
        logger.debug('取消,站点:'+target_id)
    elif task_status==0:
        logger.debug('没有任何导航任务')



async def handle_camera(cur_station)->bool:
    if int(cur_station.split("LM")[1])>=100: # 100以上的机台 
        logger.info('当前站点不是机台站点')
        return False

    logger.debug('play dianjian ');
    # 播放机台准备点检的音频,只播放一次
    play_dianjian_sound(cur_station)

    


    logger.debug(f'当前站点{cur_station},开始摄像头识别');
    station,dev = await station_service.get_cvinfo_by_station_name(cur_station)
    timestamp = get_cur_timestamp()

    logger.debug(dev.cv_type);

    if int(cur_station.split("LM")[1])>=50 and int(cur_station.split("LM")[1])<=60: # 50 - 100 之间的机台
        continue_flag = await handle_dev_window_long_camera(station,dev,timestamp)
        return continue_flag


    if dev.cv_type == 'red_temp_blue_humi':
        continue_flag = await handle_red_temp_blue_humi(station,dev,timestamp)
        return continue_flag

    elif dev.cv_type == 'red_temp_blue_humi_2':
        continue_flag = await handle_red_temp_blue_humi_2(station,dev,timestamp)
        return continue_flag

    elif dev.cv_type == 'black_bg_white_temp_humi':
        # continue_flag = await handle_black_bg_white_temp_humi(station,dev,timestamp)
        continue_flag = await handle_black_bg_white_temp_humi_two(station,dev,timestamp)
        return continue_flag

    elif dev.cv_type == 'white_panel_green_temp':
        pass
    else:
        pass

    